package com.tz.platform.utils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

/**
 * 
 * 封装分析查询的Excel导出相关的一些方法 Date 2014-10-24
 */
public class FilesUtil
{
  
  /**
	 * 取得上传文件
	 * @param request
	 * @return
	 */
	public static List<MultipartFile> getFileList(HttpServletRequest request) {
		List<MultipartFile> files = new ArrayList<MultipartFile>();
		// 创建一个通用的多部分解析器
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
				request.getSession().getServletContext());
		if (multipartResolver.isMultipart(request)) {
			// 转换成多部分request
			MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
			// 取得request中的所有文件名
			Iterator<String> iter = multiRequest.getFileNames();
			while (iter.hasNext()) {
				// 取得上传文件
				List<MultipartFile> fileList = multiRequest.getFiles(iter.next());
				for (MultipartFile file : fileList) {
					files.add(file);
				}
			}
		}
		return files;
	}
	
	
}
